﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Component;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class ReceivableDetail : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                 BindGridView();

                //WebPartBind.BindMasterDataDDL(this.ddlStatus, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDReceivableStatus"].ToString()));
                //WebPartBind.BindTrackingDDL(this.ddlTraker);
                //BindContract();
            }
        }

        #region Private Method
        //private void BindContract()
        //{
        //    SalesOrderService salesorderServiceObj = ServiceFactory.getSalesOrderServices();
        //    OperationResult<List<tb_SalesOrder>> list = salesorderServiceObj.GetAllSalesOrder();

        //    if (list.operationSuccess)
        //    {
        //        ddlContranctId.DataSource = list.obj;
        //        ddlContranctId.DataTextField = "ContranctSN";
        //        ddlContranctId.DataValueField = "Id";
        //        ddlContranctId.DataBind();
        //    }
        //}

        //private void BindEditContract()
        //{
        //    SalesOrderService salesorderServiceObj = ServiceFactory.getSalesOrderServices();
        //    OperationResult<List<tb_SalesOrder>> list = salesorderServiceObj.GetAllSalesOrder();

        //    if (list.operationSuccess)
        //    {
        //        ddlEditContranctId.DataSource = list.obj;
        //        ddlEditContranctId.DataTextField = "ContranctSN";
        //        ddlEditContranctId.DataValueField = "Id";
        //        ddlEditContranctId.DataBind();
        //    }
        //}

        private void BindGridView()
        {
            ReceivableDetailsService receivableDetailsServiceObj = ServiceFactory.getReceivableDetailsService();
            OperationResult<List<tb_ReceivableDetail>> result = receivableDetailsServiceObj.GetAllReceivableDetails();

            if (result.operationSuccess)
            {
                gvReceivableDetailList.DataSource = result.obj;
                gvReceivableDetailList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            //WebPartBind.BindMasterDataDDL(this.ddlEditStatus, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDReceivableStatus"].ToString()));
            //WebPartBind.BindTrackingDDL(this.ddlEditTraker);
          
            //ReceivableService receivableServiceObj = ServiceFactory.getReceivableService();
            //OperationResult<tb_Receivable> resultEdit = receivableServiceObj.GetReceivableById(id);
            //if (resultEdit.operationSuccess == true)
            //{
            //    ddlEditContranctId.SelectedValue = resultEdit.obj.ContranctId.ToString();
            //    ddlEditHaveBill.SelectedValue = resultEdit.obj.HaveBill.ToString();
            //    txtEditAmount.Text = resultEdit.obj.Amount.ToString();
            //    ddlEditTraker.SelectedValue = resultEdit.obj.Traker.ToString();
            //    ddlEditRound.SelectedValue = resultEdit.obj.Round;
            //    ddlEditStatus.SelectedValue = resultEdit.obj.Status.ToString();
            //    txtEditDescription.Text = resultEdit.obj.Description;

            //    lbLastModier.Text = "TEST";
            //    lbEidtId.Text = resultEdit.obj.Id.ToString();
            //    lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            //}
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        public string GetUserNameById(string strUserId)
        {
            int userId = Int32.Parse(strUserId);
            return MasterDateString.GetUserNameById(userId);
        }
        public string GetMasterDataNameById(string strTypeId)
        {
            int masterDataId = Int32.Parse(strTypeId);
            return MasterDateString.GetMasterDataNameById(masterDataId);
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ReceivableService receivableServiceObj = ServiceFactory.getReceivableService();
            tb_Receivable instance = new tb_Receivable();

            //instance.ContranctId = Int32.Parse(ddlContranctId.SelectedValue);
            //instance.HaveBill = Int32.Parse(ddlHaveBill.SelectedValue);
            //instance.Amount = decimal.Parse(txtAmount.Text);
            //instance.Traker = Int32.Parse(ddlTraker.SelectedValue);
            //instance.Round = ddlRound.SelectedValue;
            //instance.Status = Int32.Parse(ddlStatus.SelectedValue);
            //instance.Description = txtDescription.Text;

            OperationResult<tb_Receivable> result = receivableServiceObj.CreateReceivable(instance);
            BindGridView();
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            ReceivableService receivableServiceObj = ServiceFactory.getReceivableService();
            tb_Receivable entity = new tb_Receivable();

            entity.Id = editId;

            //entity.ContranctId = Int32.Parse(ddlEditContranctId.SelectedValue);
            //entity.HaveBill = Int32.Parse(ddlEditHaveBill.SelectedValue);
            //entity.Amount = decimal.Parse(txtEditAmount.Text);
            //entity.Traker = Int32.Parse(ddlEditTraker.SelectedValue);
            //entity.Round = ddlEditRound.SelectedValue;
            //entity.Status = Int32.Parse(ddlEditStatus.SelectedValue);
            //entity.Description = txtEditDescription.Text;

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_Receivable> resultEdit = receivableServiceObj.ModifyReceivable(entity);
            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            ReceivableService receivableServiceObj = ServiceFactory.getReceivableService();
            tb_Receivable deleteEntity = new tb_Receivable();
            deleteEntity.Id = deleteId;
            receivableServiceObj.DeleteReceivable(deleteEntity);
            BindGridView();
        }

        protected void gvReceivableDetailList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvReceivableDetailList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvReceivableDetailList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
